# 1 "../Codes/Vertical/temperature_control_sub_module/pid.c"
# 1 "E:\\APT_vertical_mode\\apt_vertical_mode\\APT32F102_Vertical_1.6F\\Source//"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "../Codes/Vertical/temperature_control_sub_module/pid.c"
# 1 "../Codes/Vertical/temperature_control_sub_module/pid.h" 1


# 1 "include/apt32f102_ck801.h" 1
# 85 "include/apt32f102_ck801.h"
typedef enum IRQn
{

        ISR_Restart = -32,
        ISR_Misaligned_Access = -31,
        ISR_Access_Error = -30,
        ISR_Divided_By_Zero = -29,
        ISR_Illegal = -28,
        ISR_Privlege_Violation = -27,
        ISR_Trace_Exection = -26,
        ISR_Breakpoint_Exception = -25,
        ISR_Unrecoverable_Error = -24,
        ISR_Idly4_Error = -23,
        ISR_Auto_INT = -22,
        ISR_Auto_FINT = -21,
        ISR_Reserved_HAI = -20,
        ISR_Reserved_FP = -19,
        ISR_TLB_Ins_Empty = -18,
        ISR_TLB_Data_Empty = -17,

        INTC_CORETIM_IRQn = 0,
        INTC_TIME1_IRQn = 1,
        INTC_UART0_IRQn = 2,
        INTC_GPIOA2_IRQn = 8,
} IRQn_Type;


void INTC_Init(void);
void force_interrupt(IRQn_Type IRQn);

void CK_CPU_EnAllNormalIrq(void);
void CK_CPU_DisAllNormalIrq(void);
# 4 "../Codes/Vertical/temperature_control_sub_module/pid.h" 2

typedef struct
{
 double i32Q12_Kp_coef;
 double i32Q12_Ki_coef;
    double i32Q12_Kp;
    double i32Q12_Ki;
 double i32Q12_Kd;
    double i32Q27_OutMin;
    double i32Q27_OutMax;

} stc_pid_para_t;



typedef struct
{
 char u8En;
 int i32Q15_Ref;
 int i32Q15_Fbk;
 double i32Q27_Output;
 int i32Q15_Err;

 double i32Q27_Iout;
 int i32Q27_FdFwd;
 char u8IntegralLmtFlg;
 int i32Q15_LastErr;
 int i32Q15_LastErrInc;
}stc_pi_t;

extern stc_pid_para_t pid_para_t;

void PI_Ctrl(stc_pi_t *pstc, stc_pid_para_t *stcPIParam,int grill_temperature,int temp_set);
# 2 "../Codes/Vertical/temperature_control_sub_module/pid.c" 2






void PI_Ctrl(stc_pi_t *pstc, stc_pid_para_t *stcPIParam,int grill_temperature,int temp_set)
{
 pstc->i32Q15_Fbk = grill_temperature;
 pstc->i32Q15_Ref = temp_set;

 if(pstc->u8En==0)
 {


  pstc->u8IntegralLmtFlg=0;
 }
 else
 {

  pstc->i32Q15_Err = pstc->i32Q15_Ref - pstc->i32Q15_Fbk;

  if(pstc->u8IntegralLmtFlg==0)
    pstc->i32Q27_Iout += (stcPIParam->i32Q12_Ki * pstc->i32Q15_Err);

  int i32Q15_Derivative = pstc->i32Q15_Err - pstc->i32Q15_LastErr;
  pstc->i32Q27_Output = pstc->i32Q27_Iout + (stcPIParam->i32Q12_Kp * pstc->i32Q15_Err)+(stcPIParam->i32Q12_Kd * i32Q15_Derivative);
  if(pstc->i32Q27_Output > stcPIParam->i32Q27_OutMax)
  {
   pstc->i32Q27_Output = stcPIParam->i32Q27_OutMax;
   pstc->u8IntegralLmtFlg=1;
  }
  else if(pstc->i32Q27_Output < stcPIParam->i32Q27_OutMin)
  {
   pstc->i32Q27_Output = stcPIParam->i32Q27_OutMin;
   pstc->u8IntegralLmtFlg=2;
  }
  else
  {
   pstc->u8IntegralLmtFlg=0;
  }
  if(pstc->i32Q27_Iout > stcPIParam->i32Q27_OutMax) pstc ->i32Q27_Iout = stcPIParam->i32Q27_OutMax;
  if(pstc->i32Q27_Iout < stcPIParam->i32Q27_OutMin) pstc->i32Q27_Iout = stcPIParam->i32Q27_OutMin;
  pstc->i32Q15_LastErr = pstc->i32Q15_Err;
 }
}
